package cn.glutnn.controller;


import cn.glutnn.entity.Reserve;
import cn.glutnn.entity.Result;
import cn.glutnn.service.ReserveService;
import cn.glutnn.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * (Reserve)表控制层
 *
 * @author msh
 * @since 2024-10-25 09:36:26
 */
@RestController
@RequestMapping("/reserve")
@Tag(name = "预约管理模块")
public class ReserveController {
    /**
     * 服务对象
     */
    @Autowired
    private ReserveService reserveService;

    @PostMapping("/add")
    @Operation(summary = "添加预约")
    public Result add(@RequestBody Reserve reserve, BindingResult bindingResult) {
        //检查项目
        if (bindingResult.hasErrors()) {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for (ObjectError objectError : allErrors) {
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }
            String s = stringBuffer.toString();
            System.out.println(s);
            return new Result<String>(1, "添加失败", null);
        }
        Map o = ThreadLocalUtil.get();
        String username = (String) o.get("username");
        reserve.setQuestioner(username);
        reserveService.save(reserve);
        return new Result<String>(0, "添加成功", null);
    }

    @Operation(summary = "根据ID查询预约情况")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Reserve reserve = reserveService.getById(id);
        return new Result<Reserve>(0, "查询成功", reserve);
    }

    @Operation(summary = "根据id修改预约情报")
    //根据id修改预约情报
    @PutMapping("/updateById")
    public Result updateById(@RequestBody Reserve reserve, BindingResult bindingResult) {
        //检查项目
        if (bindingResult.hasErrors()) {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for (ObjectError objectError : allErrors) {
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }
            String s = stringBuffer.toString();
            System.out.println(s);
            return new Result<String>(1, "修改失败", null);
        }
        reserveService.updateById(reserve);
        return new Result<String>(0, "修改成功", null);
    }

    //根据id删除预约情报
    @Operation(summary = "根据id删除预约情报")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        reserveService.removeById(id);
        return new Result<String>(0, "删除成功", null);
    }

    //根据用户查询预约情况 kind
    @Operation(summary = "根据用户查询预约情况")
    @GetMapping("/selectByUsername/{kind}")
    public Result selectByKind(@PathVariable("kind") String kind) {
        List<Reserve> list = reserveService.selectByKind(kind);
        return new Result<List<Reserve>>(0, "查询成功", list);
    }
}

